package dmxGroupControl.listeners;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import dmxGroupControl.Universe;
import oscP5.OscArgument;
import oscP5.OscMessage;

public class OscMessageMapping extends Mapping
{
	Pattern pattern;
	
	public OscMessageMapping(Universe universe, String pattern, String attribute)
	{
		super(universe, attribute);
		this.pattern = Pattern.compile(pattern);
	}

	@Override
	public String GetPattern()
	{
		return pattern.pattern();
	}

	@Override
	public void SetPattern(String value)
	{
		pattern = Pattern.compile(value);
	}

	@Override
	public boolean Matches(Object matchTo)
	{
		if(matchTo instanceof OscMessage)
		{
			OscMessage message = (OscMessage)matchTo;
			String address = message.addrPattern();
			Matcher matcher = pattern.matcher(address);
			return (matcher.lookingAt());
		}
		return false;
	}

	@Override
	public int GetGroup(Object matchTo, int groupOffset)
	{
	    if(matchTo instanceof OscMessage)
        {
            OscMessage message = (OscMessage)matchTo;
            String address = message.addrPattern();
            Matcher matcher = pattern.matcher(address);
            matcher.lookingAt();
            return Integer.parseInt(matcher.group(1)) - groupOffset;
        }
        return 0;
	}

	@Override
	public float GetValue(Object matchTo)
	{
	    if(matchTo instanceof OscMessage)
        {
            OscMessage message = (OscMessage)matchTo;
            OscArgument argument = message.get(0);
            return argument.floatValue();
        }
        return 0;
	}
}
